package com.toogu.mazhangmen.ui.user;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.service.DestoryService;
import com.toogu.mazhangmen.service.SockService;
import com.toogu.mazhangmen.ui.login.LoginActivity;
import com.toogu.mazhangmen.util.TokenUtil;

public class UserActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private LinearLayout layout;
    private ImageView imageView;
    private TokenUtil tokenUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉状态栏
        DestoryService.add(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        tokenUtil=new TokenUtil();
        layout=findViewById(R.id.layout);
        toolbar=findViewById(R.id.toolbar);
        imageView=findViewById(R.id.head_icon);

        layout.addView(createText("昵称",tokenUtil.getName().equals("")?"无":tokenUtil.getName()));
        layout.addView(createText("电话",tokenUtil.getMobile().equals("")?"无":tokenUtil.getMobile()));
        layout.addView(createText("邮箱",tokenUtil.getEmail().equals("")?"无":tokenUtil.getEmail()));
        layout.addView(createText("状态",tokenUtil.getStatus().equals("1")?"正常":"禁用"));

        Glide.with(this)
                .load(tokenUtil.getAvatar_url())
                .fallback( R.drawable.ic_head_icon)
                .placeholder(R.drawable.ic_head_icon)
                .diskCacheStrategy( DiskCacheStrategy.NONE )//设置图片不加入到磁盘缓存
                .skipMemoryCache(true)//跳过
                .transforms(new CircleCrop()) // 圆形图片
                .into(imageView);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    @Override
    protected void onDestroy() {
        DestoryService.remove(this);
        super.onDestroy();
    }

    private View createText(String name, String content) {
        final View view = LayoutInflater.from(this).inflate(R.layout.item_message, null, false);
        TextView nameTextView = view.findViewById(R.id.name);
        TextView contentTextView = view.findViewById(R.id.content);
        nameTextView.setText(name);
        contentTextView.setText(content);
        return view;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 0, "退出账号").setShowAsAction(1);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case 1:
                new TokenUtil().setClear();
                stopService(new Intent(this, SockService.class));
                startActivity(new Intent(UserActivity.this, LoginActivity.class));
                finish();
                break;
            default:
                break;
        }
        return true;
    }
}